#version 330 core
in vec2 TexCoord;
in vec3 Normal;
in vec3 FragPos;

out vec4 FragColor;

uniform vec3 lightPos;
uniform vec3 lightColor;
uniform vec3 diffuseColor;
uniform int useTexture;
uniform sampler2D diffuseTexture;

void main() {
    vec4 baseColor;
    if (useTexture == 1) {
        baseColor = texture(diffuseTexture, TexCoord);
    } else {
        baseColor = vec4(diffuseColor, 1.0);
    }

    // 环境光
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    // 标准化法线
    vec3 norm = normalize(Normal);

    // 计算光照方向
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // 组合光照
    vec3 result = (ambient + diffuse) * baseColor.rgb;
    FragColor = vec4(result, baseColor.a);
}